package xyz.eden.boot.common.web.support.infrastructure.gateway.base;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.eden.boot.common.web.support.domain.gateway.BaseGateway;
import xyz.eden.boot.common.web.support.domain.gateway.bo.BaseInfoBO;
import xyz.eden.boot.common.web.support.domain.gateway.bo.BaseQueryBO;
import xyz.eden.boot.common.web.support.infrastructure.resource.db.po.BasePO;
import xyz.eden.boot.common.web.support.infrastructure.resource.service.param.BaseQryParam;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * BaseGatewayImpl
 *
 * @author gavin
 * @version 1.0
 * @descriptor
 * @date 2023/4/29 17:21
 */

public abstract class BaseGatewayImpl<Qry extends BaseQueryBO, BO extends BaseInfoBO,
        PO extends BasePO, QP extends BaseQryParam, Resource extends IBaseResource<PO, QP>>
        implements BaseGateway<Qry, BO> {


    @Autowired
    private Resource resource;

    @Override
    public BO getInfoById(Long id) {
        PO po = resource.getById(id);
        return this.builderBO(po);
    }

    @Override
    public List<BO> getInfoByParam(Qry q) {
        LambdaQueryWrapper<PO> queryWrapper = this.builderInfoQueryParam(q);
        if (Objects.isNull(queryWrapper)) {
            return Collections.emptyList();
        }

        List<PO> poList = resource.list(queryWrapper);
        return Optional.of(poList).orElse(Collections.emptyList()).stream().map(this::builderBO).filter(Objects::nonNull).toList();
    }

    protected abstract BO builderBO(PO po);

    protected abstract LambdaQueryWrapper<PO> builderInfoQueryParam(Qry qry);


}
